package atguigu.time;

import java.time.*;
import java.util.Date;

public class Demo {

    public static LocalDateTime date2localDateTime(Date date) {
        if (date == null)
            return null;
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime;
    }

    public static Date localDate2Date(LocalDate localDate) {
        if (localDate == null)
            return new Date();
        Instant instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    public static void main(String[] args) {

        // Date time = null;
        // try {
        // time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2023-3-15
        // 16:26:23");
        // LocalDateTime localDateTime = date2localDateTime(time);
        // LocalDate localDate = localDateTime.toLocalDate();
        // LocalDate now = LocalDate.now();
        // if(localDate.isEqual(now)){
        // System.out.println("true");
        // }else{
        // System.out.println("false");
        // }
        // } catch (ParseException e) {
        // throw new RuntimeException(e);
        // }

        // String str = "2023-04-15 07:40:35";
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // Date time = sdf.parse(str);
            //
            // if(isInOneHours(time)){
            // System.out.println("true");
            // }else {
            // System.out.println("false");
            // }

            // if(isInOneHours(time)){
            // System.out.println("true");
            // }else{
            // System.out.println("false");
            // }

            // if(isCurrentDay(time)){
            // System.out.println("true");
            // }else{
            // System.out.println("false");
            // }

            LocalDate now = LocalDate.now();
            Date date = localDate2Date(now);
            System.out.println(date);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static boolean isCurrentDay(Date time) {
        if (time == null)
            return false;
        LocalDateTime localDateTime = date2localDateTime(time);
        LocalDate localDate = localDateTime.toLocalDate();
        LocalDate now = LocalDate.now();
        if (localDate.equals(now)) {
            return true;
        }
        return false;
    }

    public static boolean isInOneHours(Date time) {
        if (time == null)
            return false;
        LocalDateTime msgDt = date2localDateTime(time);
        LocalDate msgD = msgDt.toLocalDate();
        LocalTime msgT = msgDt.toLocalTime();
        int hour = msgT.getHour();
        System.out.println("msgH:" + hour);
        LocalDateTime nowDt = LocalDateTime.now();
        LocalDate nD = nowDt.toLocalDate();
        LocalTime nT = nowDt.toLocalTime();
        int hour1 = nT.getHour();
        System.out.println("nowH:" + hour1);
        if (msgD.equals(nD) && hour == hour1) {
            return true;
        }
        return false;
    }

    public static boolean isInHHours(Date time, int h) {
        boolean flag = false;
        if (time == null)
            return false;
        LocalDateTime msgDt = date2localDateTime(time);
        LocalDate msgD = msgDt.toLocalDate();
        LocalTime msgT = msgDt.toLocalTime();
        int hour = msgT.getHour();
        System.out.println("msgH:" + hour);

        LocalDateTime nowDt = LocalDateTime.now();
        LocalDate nD = nowDt.toLocalDate();
        LocalTime nT = nowDt.toLocalTime();
        int hour1 = nT.getHour();
        if (hour1 == h) {
            return true;
        }
        return flag;
    }

    public static boolean isInTwoHours(Date time) {
        if (time == null)
            return false;
        LocalDateTime msgDt = date2localDateTime(time);
        LocalDate msgD = msgDt.toLocalDate();
        LocalTime msgT = msgDt.toLocalTime();
        int hour = msgT.getHour();
        System.out.println("msgH:" + hour);
        LocalDateTime nowDt = LocalDateTime.now();
        LocalDate nD = nowDt.toLocalDate();
        LocalTime nT = nowDt.toLocalTime();
        int hour1 = nT.getHour();
        System.out.println("nowH:" + hour1);
        if (msgD.equals(nD) && Math.abs(hour - hour1) <= 2) {
            return true;
        }
        return false;
    }

    public static boolean isInTenMins(Date time) {
        if (time == null)
            return false;
        LocalDateTime msgDt = date2localDateTime(time);
        LocalDate msgD = msgDt.toLocalDate();
        LocalTime msgT = msgDt.toLocalTime();
        int hour = msgT.getHour();
        int minute = msgT.getMinute();

        LocalDateTime nowDt = LocalDateTime.now();
        LocalDate nD = nowDt.toLocalDate();
        LocalTime nT = nowDt.toLocalTime();
        int hour1 = nT.getHour();
        int minute1 = nT.getMinute();
        System.out.println("msgH:" + hour);
        System.out.println("nowH:" + hour1);
        System.out.println("msgM:" + minute);
        System.out.println("nowM:" + minute1);
        if (msgD.equals(nD) && hour == hour1 && Math.abs(minute - minute1) <= 10) {
            return true;
        }
        return false;
    }
}
